﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Windows.Forms;

namespace AppImporter
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Check if this is first time the application is run, if so check if all dll's are in place.
            if(Properties.Settings.Default.FirstRun == "true")
            {
                if (File.Exists(@".\AdminUI.WqlQueryEngine.dll").Equals(false) || File.Exists(@".\DcmObjectModel.dll").Equals(false) || File.Exists(@".\Microsoft.ConfigurationManagement.ApplicationManagement.dll").Equals(false)
                     || File.Exists(@".\Microsoft.ConfigurationManagement.ApplicationManagement.MsiInstaller.dll").Equals(false) || File.Exists(@".\Microsoft.ConfigurationManagement.ManagementProvider.dll").Equals(false))
                //if (File.Exists(@".\AdminUI.WqlQueryEngine2.dll").Equals(false) || File.Exists(@".\DcmObjectModel.dll").Equals(false))
                {
                    if (MessageBox.Show("One of the following files are missing:\n" +
                        "AdminUI.WqlQueryEngine.dll \n" +
                        "DcmObjectModel.dll \n" +
                        "Microsoft.ConfigurationManagement.ApplicationManagement.dll \n" +
                        "Microsoft.ConfigurationManagement.ApplicationManagement.MsiInstaller.dll \n" +
                        "Microsoft.ConfigurationManagement.ManagementProvider.dll \n" +
                        "If you choose Ok, make sure you ran the program admin/elevated so you have proper rights to copy files to the installed dir \n\n" +
                        "Press Ok to browse to where they can be found or Cancel to quit and copy them manually"
                        , "Files not found", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        FolderBrowserDialogEx MissingFiles = new FolderBrowserDialogEx();
                        
                        MissingFiles.Description = "Path to bin folder where SCCM Console is installed";
                        MissingFiles.SelectedPath = @"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin";
                        MissingFiles.ShowDialog();
                        //MessageBox.Show(MissingFiles.SelectedPath.ToString() + @"\AdminUI.WqlQueryEngine.dll");
                        try
                        {
                            // Copy all needed files from SCCM Console installation dir.
                            File.Copy(MissingFiles.SelectedPath.ToString() + @"\AdminUI.WqlQueryEngine.dll", @".\AdminUI.WqlQueryEngine.dll");
                            File.Copy(MissingFiles.SelectedPath.ToString() + @"\DcmObjectModel.dll", @".\DcmObjectModel.dll");
                            File.Copy(MissingFiles.SelectedPath.ToString() + @"\Microsoft.ConfigurationManagement.ApplicationManagement.dll", @".\Microsoft.ConfigurationManagement.ApplicationManagement.dll");
                            File.Copy(MissingFiles.SelectedPath.ToString() + @"\Microsoft.ConfigurationManagement.ApplicationManagement.MsiInstaller.dll", @".\Microsoft.ConfigurationManagement.ApplicationManagement.MsiInstaller.dll");
                            File.Copy(MissingFiles.SelectedPath.ToString() + @"\Microsoft.ConfigurationManagement.ManagementProvider.dll", @".\Microsoft.ConfigurationManagement.ManagementProvider.dll");
                        }
                        catch
                        {
                            MessageBox.Show("Some or all of the files could not be found, wrong directory? \n" +
                                "Default path is: \n" +
                                @"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin" + "\n" +
                                "Application Will now quit.", "Files Missing");
                            Environment.Exit(0);
                        }
                        finally
                        {

                        }
                    }
                    else
                    {
                        //MessageBox.Show("Exit");
                        Environment.Exit(0);
                    }
                }
                Properties.Settings.Default.FirstRun = "false";
            }
            
            Application.Run(new MainForm());
        }
    }
}
